package fr.upmc.mdoc.tp1.tools;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.TilesRequestProcessor;

public class AuthController extends TilesRequestProcessor {

	// On verifie si l'utilsateur est bien connect�
	protected boolean isUserConnected(HttpServletRequest request) {
		
		if (request == null) return false;
		
		HttpSession session = request.getSession(false);
		String user = null;
		if (session != null)
			user = (String) session.getAttribute("user");
		
		return user != null;
		
	}
	
	protected boolean isSessionValid(HttpServletRequest request)
			throws ServletException {
		if (request != null) {
			return request.isRequestedSessionIdValid();
		}
		return false;
	}
	
	
	
	@Override
	protected boolean processRoles(HttpServletRequest request,
			HttpServletResponse response, ActionMapping mapping)
			throws java.io.IOException, javax.servlet.ServletException {
		
		//ActionMessages messages = new ActionMessages();
		
		//if (isSessionValid(request) && isUserConnected(request)) 
		//	return true;
		
		//else if (!request.getRequestURI().endsWith("Connect.do"))
		//	request.getRequestDispatcher("/Connect.do").forward(request, response);
		
				//messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
				//		"erreurs.connection.required"));
		// rediriger vers une page d'erreur

		boolean process = super.processRoles(request, response, mapping);
		
		/*System.out.println(request.getRequestURL());
		
		if (isSessionValid(request) && isUserConnected(request)) 
			return true;
		else if (!request.getRequestURL().toString().endsWith("Connect.do"))
			request.getRequestDispatcher("/Connect.do").forward(request, response);
		*/
		
		return process;
		
	}

}
